package com.hzvtc.bosswang.manageuser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzvtc.bosswang.managecommon.helper.StringHelper;
import com.hzvtc.bosswang.manageuser.domain.User;

import com.hzvtc.bosswang.manageuser.mapper.UserMapper;
import com.hzvtc.bosswang.manageuser.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public User selectUserByStudentId(Long id) {
        return userMapper.selectUserByStudentId(id);
    }

    @Override
    public IPage<User> selectUserList(Page<User> page,User user) {


        return userMapper.selectUserList(page,user);
    }

    @Override
    public User selectUserById(Long id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public Integer editUser(User user) {
        if (user.getPwd()!=null){
            String pwdSalt= UUID.randomUUID().toString().replace("-","");
            String inputPwd=user.getPwd();
            String pwd=StringHelper.generateMD5(inputPwd+pwdSalt);
            user.setPwd(pwd);
            user.setPwdSalt(pwdSalt);
        }
        return userMapper.edit(user);
    }

    @Override
    public Integer deleteUser(Long id) {
        return userMapper.delete(id);
    }

    @Override
    public Boolean login(User user) {
        User selectedUser = userMapper.selectUserByUserName(user.getUserName());
        String pwdSalt= selectedUser.getPwdSalt();
        String inputPwd=user.getPwd();
        String pwd= StringHelper.generateMD5(inputPwd+pwdSalt);
        System.out.println(user);
        System.out.println(selectedUser);
        if (!pwd.equals(selectedUser.getPwd())||!user.getUserType().equals(selectedUser.getUserType())){
            selectedUser.setPwdErrorCount(selectedUser.getPwdErrorCount() + 1);
            userMapper.edit(selectedUser);
            return false;
        }else {
            selectedUser.setPwdErrorCount(0);
            userMapper.edit(selectedUser);
        }
        redisTemplate.opsForValue().set("session:"+selectedUser.getUserName(),user.toString(),10, TimeUnit.MINUTES);
        return true;

    }

    @Override
    public Integer isValidate(String userName) {
        User user = userMapper.selectUserByUserName(userName);
        if (user!=null){
            return user.getPwdErrorCount();
        }
        return -1;
    }

    @Override
    public Boolean isLogin(String userName){
        if (userMapper.selectUserByUserName(userName)!=null){
            return redisTemplate.opsForValue().get("session:"+userName)!=null;
        }
        return false;

    }

    @Override
    public Integer addUser(User user) {
        User selectedUser = userMapper.selectUserByUserName(user.getUserName());
        if (selectedUser!=null){
            return 0;
        }
        String inputPwd = user.getPwd();
        String pwdSalt= java.util.UUID.randomUUID().toString().replace("-","");
        user.setPwdSalt(pwdSalt);
        user.setPwd(StringHelper.generateMD5(inputPwd+pwdSalt));
        user.setPwdValidDate(System.currentTimeMillis()+3600*24*7*1000);
        user.setPwdErrorCount(0);
        return userMapper.add(user);
    }
}
